package com.boco.modules.fdoc.web.organization;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.print.Doc;
import javax.servlet.http.HttpServletRequest;

import org.apache.zookeeper.server.quorum.Leader;
import org.apache.zookeeper.txn.Txn;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.dubbo.common.json.JSON;
import com.boco.common.enums.ApiStatusEnum;
import com.boco.common.json.BaseJsonVo;
import com.boco.common.persistence.Page;
import com.boco.common.utils.JsonUtils;
import com.boco.common.utils.StringUtils;
import com.boco.modules.fdoc.model.DoctorEntity;
import com.boco.modules.fdoc.model.HospitalEntity;
import com.boco.modules.fdoc.service.DepartmentService;
import com.boco.modules.fdoc.service.DocService;
import com.boco.modules.fdoc.service.HospitalService;
import com.boco.modules.fdoc.vo.DoctorDetailVo;

/**
 * Description：医生团队controller
 * @author lzz
 *
 */
@Controller
@RequestMapping(value="/team",produces = "application/json;charset=UTF-8")
public class TeamController {
	
	@Resource
	HospitalService hospitalService;
	@Resource
	DocService docService;
	@Resource
	DepartmentService deptService;
	
	/**
	 * 跳转团队首页
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/showPage",method = RequestMethod.GET)
	public String showPage(HttpServletRequest request, Model model){
		//获取医院列表
		List<HospitalEntity> hospitalList = hospitalService.getHospitalList();
		//设置作用域对象
		model.addAttribute("hospitalList", hospitalList);
		model.addAttribute("hospitalList2",JsonUtils.getJson(hospitalList));
		return "/organization/team";
	}

	/**
	 * 获取团队的队长列表
	 * @param request
	 * @param model
	 * @param orgId 医院id
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="/getTeamLeader",method=RequestMethod.GET)
	public String getTeamLeader(HttpServletRequest request, Model model,String orgId){
		DoctorDetailVo docVo=new DoctorDetailVo();
		docVo.setOrgId(orgId);
		List<DoctorDetailVo> dlist=docService.getLeaderList(docVo);
		return JsonUtils.getJson(dlist);
	}
	
	/**
	 * 根据条件获取团队列表
	 * @param request
	 * @param model
	 * @param orgId 医院id
	 * @param teamId 团队id
	 * @param pageSize 页码
	 * @param pageIndex 页面大小
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="/getTeamList",method=RequestMethod.GET)
	public String getTeamList(HttpServletRequest request, Model model,String orgId,String teamId,
			Integer pageSize,Integer pageIndex){
		DoctorDetailVo docVo=new DoctorDetailVo();
		Page<DoctorDetailVo> page=new Page<DoctorDetailVo>(pageIndex, pageSize);
		docVo.setPage(page);
		docVo.setOrgId(orgId);
		docVo.setTeamId(teamId);
		
		DoctorDetailVo countVo=new DoctorDetailVo();
		countVo.setOrgId(orgId);
		countVo.setTeamId(teamId);
		
		int count=docService.getLeaderCount(countVo);
		List<DoctorDetailVo> dlist=docService.getLeaderList(docVo);
		List<Map<String, Object>> rList=new ArrayList<Map<String,Object>>();
		for(DoctorDetailVo dVo:dlist){
			Map<String, Object> paraMap=new HashMap<String,Object>();
			paraMap.put("leader", dVo.getDocName());
			paraMap.put("teamId", dVo.getTeamId());
			
			//根据团队Id获取团队数据
			List<DoctorDetailVo> listtwo=docService.getDoctorTeamMemberByTeamId(dVo.getTeamId());
			String docOne="";
			String docTwo="";
			String docThree="";
			for(DoctorDetailVo doVo:listtwo){
				if("1".equals(doVo.getDocType())){
					docOne=docOne+" "+doVo.getDocName();
					paraMap.put("docOne", docOne);
				}else if("2".equals(doVo.getDocType())){
					docTwo=docTwo+" "+doVo.getDocName();
					paraMap.put("docTwo", docTwo);
				}else if("3".equals(doVo.getDocType())){
					docThree=docThree+" "+doVo.getDocName();
					paraMap.put("docThree", docThree);
				}
			}
			rList.add(paraMap);
		}
		return JsonUtils.getJson(BaseJsonVo.pageList(rList, count));
	}
	
	/**
	 * 删除团队(占未使用，未处理团队删除后的数据迁移)
	 * @param request
	 * @param model
	 * @param teamId
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="/deleteTeam",method=RequestMethod.POST)
	public String deleteTeam(HttpServletRequest request, Model model,String teamId){
		int flag=docService.deleteTeam(teamId);
		if(flag>0){
			return JsonUtils.getJsonFormat(BaseJsonVo.success(null));
		}else{
			return JsonUtils.getJsonFormat(BaseJsonVo.empty(ApiStatusEnum.FAIL.getKey(), ApiStatusEnum.FAIL.getValue()));
		}
	}
	
	/**
	 * 跳转新增团队页面
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping(value="/toAddTeam",method=RequestMethod.GET)
	public String toAddTeam(HttpServletRequest request, Model model){
		//获取医院列表
		List<HospitalEntity> hospitalList = hospitalService.getHospitalList();
		//设置作用域对象
		model.addAttribute("hospitalList", hospitalList);
		return "/organization/addTeam";
	}
	
	/**
	 * 根据医生类型获取当前医院下医生列表
	 * @param request
	 * @param model
	 * @param orgId 医院id
	 * @param docType 医生类型
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="/getDocTypeList",method=RequestMethod.GET)
	public String getDocOne(HttpServletRequest request, Model model,String orgId,String docType){
		DoctorDetailVo docVo=new DoctorDetailVo();
		docVo.setOrgId(orgId);
		List<DoctorDetailVo> list=docService.getDocList(docVo);
		List<DoctorDetailVo> list2=new ArrayList<DoctorDetailVo>();
		for(DoctorDetailVo dVo: list){
			if(docType.equals(dVo.getDocType())){
				list2.add(dVo);
			}
		}
		return JsonUtils.getJson(list2);
	}
	
	/**
	 * 保存信息
	 * @param request
	 * @param model
	 * @param sden
	 * @return
	 */
	@Transactional
	@ResponseBody
	@RequestMapping(value="/saveTeam",method=RequestMethod.POST)
	public String saveTeam(HttpServletRequest request, Model model,String sden){
		DoctorDetailVo docVo=new DoctorDetailVo();
		List<DoctorDetailVo> list=docService.getDocList(docVo);
		int teamId=0;
		for(DoctorDetailVo dVo:list){
			
			if(dVo.getTeamId()==null || "".equals(dVo.getTeamId())){
				continue;
			}else{
				int tid=Integer.parseInt(dVo.getTeamId());
				if(tid>teamId){
					teamId=tid;
				}
			} 
		}
		teamId=teamId+1;
		List<Map> returnList=JsonUtils.getObjectList(sden, Map.class);
		Integer flag=1;
		i:for(Map m1:returnList){
			String id=(String) m1.get("docId");
			DoctorEntity dEntity=docService.getDocEntity(id);
			if("1".equals(dEntity.getLeaderFlag())){
				return JsonUtils.getJsonFormat(BaseJsonVo.empty(ApiStatusEnum.FAIL.getKey(), dEntity.getDocName()+"是其他团队的队长，无法直接修改。请先去该团队修改团队队长"));
			}
			
			DoctorEntity doctorEntity=new DoctorEntity();
			if(((String)m1.get("leaderFlag"))!="" && ((String)m1.get("leaderFlag"))!=null){
				doctorEntity.setLeaderFlag((String)m1.get("leaderFlag"));
			}else{
				doctorEntity.setLeaderFlag(null);
			}
			doctorEntity.setId((String) m1.get("docId"));
			doctorEntity.setTeamId(String.valueOf(teamId));
			doctorEntity.setUpdateTime(new Date());
			flag=docService.saveTeam(doctorEntity);
			if(flag==0){
				break i;
			}
		}
		if(flag>0){
			 return JsonUtils.getJsonFormat(BaseJsonVo.success(null));
		}else{
			 return JsonUtils.getJsonFormat(BaseJsonVo.empty(ApiStatusEnum.FAIL.getKey(), ApiStatusEnum.FAIL.getValue()));
		}
		
	}
	
	/**
	 * 跳转修改团队页面
	 * @param request
	 * @param model
	 * @param teamId
	 * @return
	 */
	@RequestMapping(value="/toUpdateTeam",method=RequestMethod.GET)
	public String toUpdateTeam(HttpServletRequest request, Model model ,String teamId){
		
		List<DoctorDetailVo> docList=docService.getDoctorTeamMemberByTeamId(teamId);
		String leader="";
		String orgId="";
		for(DoctorDetailVo docVo:docList){
			if("1".equals(docVo.getLeaderFlag())){
				leader=docVo.getId();
				orgId=docVo.getOrgId();
			}
			DoctorDetailVo dVo=new DoctorDetailVo();
			dVo.setOrgId(docVo.getOrgId());
			List<DoctorDetailVo> list=docService.getDocList(dVo);
			List<DoctorDetailVo> list2=new ArrayList<DoctorDetailVo>();
			for(DoctorDetailVo dcVo: list){
				if((docVo.getDocType()).equals(dcVo.getDocType())){
					list2.add(dcVo);
				}
			}
			docVo.setOtherList(list2);
		}
		if("".equals(leader)){
			 return JsonUtils.getJsonFormat(BaseJsonVo.empty(ApiStatusEnum.FAIL.getKey(), ApiStatusEnum.FAIL.getValue()));
		}
		
		model.addAttribute("docList", JsonUtils.getJson(docList));
		model.addAttribute("leader", leader);
		model.addAttribute("orgId", orgId);
		model.addAttribute("teamId", teamId);
		//获取医院列表
		List<HospitalEntity> hospitalList = hospitalService.getHospitalList();
		//设置作用域对象
		model.addAttribute("hospitalList", hospitalList);
		return "/organization/updateTeam";
	}
	
	/**
	 * 修改团队
	 * @param request
	 * @param model
	 * @param sden 修改后的信息
	 * @param teamId 团队id
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="/updateTeam",method=RequestMethod.POST)
	public String updateTeam(HttpServletRequest request, Model model,String sden,String teamId){
		List<DoctorDetailVo> docList=docService.getDoctorTeamMemberByTeamId(teamId);
		List<Map> returnList=JsonUtils.getObjectList(sden, Map.class);
		
		Integer flag=docService.updateTeam(docList,returnList,teamId);
		if(flag>0){
			 return JsonUtils.getJsonFormat(BaseJsonVo.success(null));
		}else if(flag == -1){
			return JsonUtils.getJsonFormat(BaseJsonVo.empty(ApiStatusEnum.FAIL.getKey(),"修改中有医生是其他团队的队长，无法直接修改。请确认后先去该团队修改团队队长"));
		}else{
			 return JsonUtils.getJsonFormat(BaseJsonVo.empty(ApiStatusEnum.FAIL.getKey(), ApiStatusEnum.FAIL.getValue()));
		}
	}
}
